package com.seecen.interceptors;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.seecen.until.JwtHelper;
import com.seecen.until.Result;
import com.seecen.until.ResultCodeEnum;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginProtectedInterceptor implements HandlerInterceptor {

    @Resource
    private JwtHelper jwtHelper;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");

        boolean expiration = jwtHelper.isExpiration(token);
        if (!expiration){
            return true;
        }
        Result ret = Result.build(null, ResultCodeEnum.NOTLOGIN);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(ret);
        response.getWriter().print(json);
        return false;
    }
}
